<template>
  <div class="header">
    <div class="logo" :class="{'is-active':isActive}">
      <!--<img src="@/assets/logo.png" alt="" height="30px">-->
      电子书系统
    </div>
    <div class="navbar">
      <div class="btn" :class="{'is-active':isActive}" @click="handleMenu">
        <span :class="menuBtn"></span>
      </div>
      <el-dropdown trigger="click">
        <span class="el-dropdown-link">
          {{nickname}}<i class="el-icon-arrow-down el-icon--right"></i>
        </span>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item @click.native="handleEdit">修改密码</el-dropdown-item>
          <el-dropdown-item @click.native="logout">退出 </el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </div>

    <el-dialog :title="dialog.title" :visible.sync="dialog.dialogFormVisible" :close-on-click-modal="dialog.close">
      <el-form :model="user">
        <el-form-item label="原密码" label-width="100px">
          <el-input v-model="user.oldpwd" type="password" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="新密码" label-width="100px">
          <el-input v-model="user.newpwd" type="password" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="重输新密码" label-width="100px">
          <el-input v-model="user.newpwdagain" type="password" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialog.dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleSave">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapMutations } from 'vuex'
export default {
  name: 'Header',
  data () {
    return {
      menuBtn: 'el-icon-newfont-caidan',
      username:"",
      nickname:"",
      user:{
        oldpwd:'',
        newpwd:'',
        newpwdagain:''
      },
      dialog:{
        dialogFormVisible:false,
        title:"修改密码",
        close:false,
      },
    }
  },
  computed: {
    // 如果左侧菜单打开，则旋转btn180度
    isActive () {
      return !this.$store.getters.sidebar.opened
    }
  },
  created(){
    var storage=window.localStorage;
    var user = JSON.parse(storage["user"]);
    this.username = user.user.account;
    this.nickname = user.name;
  },
  methods: {
    ...mapMutations({
      bindLogout: 'BIND_LOGOUT',
    }),
    handleMenu () {
      this.$store.dispatch('ToggleSideBar')
    },
    logout () {
      this.$confirm('是否退出账户?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        var that = this;
        that.$api.post('/super_admin/logout', that.user, response => {
          if (response.status >= 200 && response.status < 300) {
            console.log(response.data);
            var res = response.data;
            if (res.code == 0) {
              that.$message({
                message: '退出登录成功',
                type: 'success'
              });
              that.dialog.dialogFormVisible = false;
              that.$router.push('/superAdmin/login');
            }else{
              that.$message({
                message: res.msg,
                type: 'warning'
              });
            }
          } else {
            this.$message({
              message: "服务器维护中",
              type: 'error'
            });
          }

        });
      }).catch(() => {
      })
    },

    handleEdit() {
      this.user.oldpwd = "";
      this.user.newpwd = "";
      this.user.newpwdagain = "";
      this.dialog.dialogFormVisible = true;
    },
    handleSave() {
      if(this.user.newpwd != this.user.newpwdagain){
        this.$message({
          type: 'error',
          message: '两次输入的新密码不一致'
        });
        return;
      }

      var that = this;
      that.$api.put('/super_admin/setpassword', that.user, response => {
        if (response.status >= 200 && response.status < 300) {
          console.log(response.data);
          var res = response.data;
          if (res.code == 0) {
            this.$message({
              message: '修改成功,请使用新密码登录',
              type: 'success'
            });
            that.dialog.dialogFormVisible = false;
            that.$router.push('/superAdmin/login');
          }else{
            that.$message({
              message: res.msg,
              type: 'warning'
            });
          }
        } else {
          this.$message({
            message: "服务器维护中",
            type: 'error'
          });
        }

      });
    },
  }
}
</script>

<style scoped lang="stylus">
  .header
    width 100%
    height 50px
    display flex
    background #36a9e1
    .logo
      width 200px
      height 50px
      background #1e8fc6
      color #fff
      text-indent 15px
      font-size 18px
      line-height 50px
      font-weight 600
      transition .4s ease
      &.is-active
        width 64px
      img
        padding 0 5px 5px 0
    .navbar
      flex 1
      display flex
      align-items center
      justify-content space-between
      padding 0 10px
      color #fff
      .btn
        height 50px
        line-height 50px;
        cursor pointer
        transition .4s ease
        &.is-active
          transform: rotateY(180deg)
        span
          font-size 28px
          line-height 50px
          transition 0.5s
          color #ffffff
          font-weight 400
      .el-dropdown-link
        color #fff
        font-weight bold
        cursor pointer

  @media all and (max-width: 768px) {
    .header .logo.is-active {
      width: 0;
      overflow: hidden;
    }
  }
</style>
